using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
using System.Data;
using System.ComponentModel;
using System.Configuration;

namespace FW
{
    public class proxyweb
    {

        private ArrayList _alCampos;
        private ArrayList _alValores;
        private string _sUrl;
        private string _sRetorno;


        public proxyweb()
        {
            _alValores = new ArrayList();
            _alCampos = new ArrayList();
        }

        private string GetMultipartBoundary()
        {
            return String.Format("***********************{0}",
            DateTime.Now.Ticks);
        }


        public string buscaResultado()
        {
            string ret = "";
            string uploadFileName;
            System.IO.FileInfo fileToUpLoad;
            string boundry;
            string databoundary;
            string filetype;
            byte[] byteArrayBefore;
            byte[] byteArray;
            byte[] byteArrayAfter;
            MemoryStream oPostStream;
            BinaryWriter oPostData;


            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(_sUrl);
            request.PreAuthenticate = true;
            request.AllowWriteStreamBuffering = true;



            oPostStream = new MemoryStream();
            oPostData = new BinaryWriter(oPostStream);
            boundry = GetMultipartBoundary();
            databoundary = "--" + boundry;

            Encoding enc = Encoding.Default;



            request.ContentType = string.Format("multipart/form-data;boundary={0}", boundry); //@"application/x-www-form-urlencoded"; //
            request.Method = "POST";

            StringBuilder reqString = new StringBuilder();

            reqString.Append("--" + boundry + "\r\n");

            for (int x = 0; x <= _alCampos.Count - 1; x++)
            {

                reqString.Append(databoundary + "\r\n");
                reqString.Append("Content-Disposition: form-data; name=\""+_alCampos[x].ToString()+"\";");
                reqString.Append("\r\n");
                reqString.Append("\r\n");
                reqString.Append(_alValores[x].ToString());
                reqString.Append("\r\n");

            }
            reqString.Append("\r\n");
            reqString.AppendFormat("--{0}--\r\n", boundry);
            
            byteArrayBefore = enc.GetBytes(reqString.ToString());


            
            oPostData.Write(byteArrayBefore);
            
            request.ContentLength = (int)oPostData.BaseStream.Length;

            try
            {
                Stream stream = request.GetRequestStream();
                oPostStream.WriteTo(stream);
                StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream());
                string myOrginalRawHtml = reader.ReadToEnd();
                myOrginalRawHtml = myOrginalRawHtml.Replace("<br>", "\r\n");
                ret = myOrginalRawHtml;
            }
            catch (Exception ex)
            {
                ret = (ex.Message);
            }


            return ret;
        }

        public void addcampo(string _nomecampo, string _valor)
        {
            _alCampos.Add(_nomecampo);
            _alValores.Add(_valor);
        }

        public string URL
        {
            get
            {
                return _sUrl;
            }
            set
            {
                _sUrl = value;
            }
        }

        public string RETORNO
        {
            get
            {
                return _sRetorno;
            }
            set
            {
                _sRetorno = value;
            }
        }
    }
}
